#!/usr/bin/env python

##  flamingo - 2D Game Engine
##  Copyright (C) 2009 Bradley Zeis
##
##  This program is free software: you can redistribute it and/or modify
##  it under the terms of the GNU General Public License as published by
##  the Free Software Foundation, either version 3 of the License, or
##  (at your option) any later version.
##
##  This program is distributed in the hope that it will be useful,
##  but WITHOUT ANY WARRANTY; without even the implied warranty of
##  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
##  GNU General Public License for more details.
##
##  You should have received a copy of the GNU General Public License
##  along with this program.  If not, see <http://www.gnu.org/licenses/>.
##
##  Bradley Zeis
##  flamingoengine@gmail.com

import flamingo as fl

class Game(fl.game.Game):
    def init(self, *args):
        self.screen1 = fl.screen.Screen((0, 0), 800, 600)
        self.add_screen(self.screen1)
        self.screen1.center = 400, 300
        
        fl.event.bind(lambda: self.data.screens[0].__setattr__('centerx', self.data.screens[0].centerx + 1),
                      fl.constants.K_d, fl.constants.KEYHOLD, mods=fl.event.Mods([], [fl.constants.K_LSHIFT]))
        fl.event.bind(lambda: self.data.screens[0].__setattr__('centerx', self.data.screens[0].centerx - 1),
                      fl.constants.K_a, fl.constants.KEYHOLD)
        fl.event.bind(lambda: self.data.screens[0].__setattr__('centery', self.data.screens[0].centery + 1),
                      fl.constants.K_w, fl.constants.KEYHOLD, delay=5)
        fl.event.bind(lambda: self.data.screens[0].__setattr__('centery', self.data.screens[0].centery - 1),
                      fl.constants.K_s, fl.constants.KEYHOLD)
        
        
        fl.event.bind(lambda: self.data.screens[0].__setattr__('zoom', self.data.screens[0].zoom * 0.96),
                      fl.constants.MS_DOWN, fl.constants.SCROLLWHEEL, mods=fl.event.Mods([fl.constants.K_LSHIFT]))
        fl.event.bind(lambda: self.data.screens[0].__setattr__('zoom', self.data.screens[0].zoom / 0.96),
                      fl.constants.MS_UP, fl.constants.SCROLLWHEEL, mods=fl.event.Mods([fl.constants.K_LSHIFT]))
        fl.event.bind(lambda: self.data.screens[0].__setattr__('zoom', self.data.screens[0].zoom * 0.96),
                      fl.constants.K_i, fl.constants.KEYHOLD)
        fl.event.bind(lambda: self.data.screens[0].__setattr__('zoom', self.data.screens[0].zoom / 0.96),
                      fl.constants.K_o, fl.constants.KEYHOLD)
        
        fl.event.bind(lambda: self.data.screens[0].__setattr__('orientation', self.data.screens[0].orientation + 1),
                      fl.constants.K_k, fl.constants.KEYHOLD)
        fl.event.bind(lambda: self.data.screens[0].__setattr__('orientation', self.data.screens[0].orientation - 1),
                      fl.constants.K_l, fl.constants.KEYHOLD)
        
        fl.event.bind(self.data.master_clock.pause, fl.constants.K_COMMA, fl.constants.KEYDOWN)
        fl.event.bind(self.data.master_clock.resume, fl.constants.K_PERIOD, fl.constants.KEYDOWN)
        
        fl.event.bind(self.quit_game, fl.constants.K_ESCAPE, fl.constants.KEYDOWN)
        
        
    def start_game(self):
        i = fl.image.load("res/flamingo.png")
        ii = fl.image.load("res/test.gif")
        iii = fl.image.load_sheeth("res/swatch.png", width=100, speed=50)
        
        s1 = fl.sprite.Sprite(i)
        s1.center = (200, 450)
        s2 = fl.sprite.Sprite(ii)
        s2.center = (400, 300)
        s2.color.a = 0.08
        s3 = fl.sprite.Sprite(iii)
        s3.center = (600, 150)
        s3.color.a = 0.08
        
        t = fl.fltime.Timer(15000)
        
        fl.event.bind(self.quit_game, t, fl.constants.TIMEREND)
        fl.event.bind(t.start, fl.constants.K_SPACE, fl.constants.KEYDOWN)
        
        def p():
            self.screen1.orientation = -(144**(t.percent() * 1.5) - 1)
            self.screen1.zoom = 0.005 + t.percent_left() * 0.995
            #s2.color.a = t.percent_left()
            s1.color.b = 1 - 0.5 * t.percent()
            s3.color.r = t.percent_left()
            s3.color.g = 1 - 0.5 * t.percent()
            
        def va():
            s1.bones[0].angle -= .03
            s1.bones[0][0].angle -= .02
            s1.bones[1].angle -= .1
            s1.bones[1][0].angle -= .03
            s1.bones[1].length -= .01
            s1.bones[2].angle -= .015
            s1.bones[2][0].angle -= .01
            s1.bones[3].angle -= -.005
            s1.bones[3][0].angle -= -.008
        def vs():
            s1.bones[0].angle += .03
            s1.bones[0][0].angle += .02
            s1.bones[1].angle += .1
            s1.bones[1][0].angle += .03
            s1.bones[1].length += .01
            s1.bones[2].angle += .015
            s1.bones[2][0].angle += .01
            s1.bones[3].angle += -.005
            s1.bones[3][0].angle += -.008
        
        fl.event.bind(va, fl.constants.K_DOWN, fl.constants.KEYHOLD)
        fl.event.bind(vs, fl.constants.K_UP, fl.constants.KEYHOLD)
        fl.event.bind(p, t, fl.constants.TIMERTICK)

if __name__ == "__main__":
    fl.util.enable_logging()
    
    try:
        import psyco
        psyco.full()
    except ImportError:
        pass

    game = Game()
    game.run(True, 0)
